package cn.hfuu.controller;

import cn.hfuu.domain.User;
import cn.hfuu.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class UserAction extends ActionSupport implements ModelDriven<User>{

    private User user = new User();
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String login() throws Exception {
        //调用server层的方法查询user对象
        User u = userService.getUserByCodePassword(user);

        //将user对象放入session域中
        ActionContext.getContext().getSession().put("user",u);


        return "login";
    }
    public String register() throws Exception {
        //调用service层方法，保存用户信息
        try {
            userService.registerUser(user);
        }catch (Exception e) {
            e.printStackTrace();
            ActionContext.getContext().put("error",e.getMessage());
            return "register";
        }

        //重定向到登陆页面
        return "toLogin";
    }

    @Override
    public User getModel() {
        return user;
    }
}
